/*
 * Created on Jun 1, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.scohen.juploadr.ui.thumbnail;

import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.widgets.Composite;
import org.scohen.juploadr.Messages;
import org.scohen.juploadr.app.ImageAttributes;
import org.scohen.juploadr.app.UploadImage;
import org.scohen.juploadr.app.tags.TagParser;
import org.scohen.juploadr.prefs.Configuration;
import org.scohen.juploadr.ui.ReusableUIFactory;

/**
 * @author steve
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class TagsThumbnailDecorator extends AbstractThumbnailDecorator {

    private static final int BORDER_TOP = 1;
    private static final int BORDER_LEFT = 2;
    private static final int BORDER_BOTTOM = 4;
    private static int TAB_WIDTH = 85;
    private static int TAB_HEIGHT = 15;

    private boolean displayText = false;
    public Color friends;
    private Color privateColor;
    private Color family;
    private Color publicColor;
    private Color description;
    private Color titleColor;
    private Color tags;

    private ReusableUIFactory uiFactory;

    public TagsThumbnailDecorator(Composite parent, ImageAttributes atts, Thumbnail t) {
        super(parent, atts, t);
        uiFactory = ReusableUIFactory.getInstance();

        friends = uiFactory.getFriendsColor();
        family = uiFactory.getFamilyColor();
        privateColor = uiFactory.getPrivateColor();
        publicColor = uiFactory.getPublicColor();
        tags = uiFactory.getTagColor();
        description = uiFactory.getDescriptionColor();
        titleColor = uiFactory.getTitleColor();
        t.addMouseTrackListener(new MouseTrackAdapter() {

            public void mouseEnter(MouseEvent event) {
                displayText = true;
                redraw();
            }

            public void mouseExit(MouseEvent event) {
                displayText = false;
                redraw();
            }
        });

    }

    void widgetDisposed(DisposeEvent e) {
    }

    void paintControl(PaintEvent event) {
        GC gc = event.gc;
        if (Configuration.getInstance().getUseImageLayovers()) {
            if (attrs.isPubliclyVisible()) {
                addPublicTab(gc);
            } else {
                addPrivateTab(gc);
                if (attrs.isFamilyViewable()) {
                    addFamilyTab(gc);
                }
                if (attrs.isFriendViewable()) {
                    addFriendsTab(gc);
                }
            }
            if (attrs.hasTags()) {
                addTagsTab(gc);
            }
            if (attrs.hasTitle()) {
                addTitleTag(gc);
            }
            if (attrs.hasDescription()) {
                addDescriptionTab(gc);
            }
        }
    }

    public void thumbnailChanged() {
        super.thumbnailChanged();
        ImageData data = thumb.getImage().getImageData();
        TAB_WIDTH = data.width;
        TAB_HEIGHT = data.height / 6;
    }

    private void addFamilyTab(GC gc) {
        int border = BORDER_LEFT;

        if (!attrs.isFriendViewable()) {
            if (displayText) {
                addTab("", gc, family, 1, BORDER_LEFT); //$NON-NLS-1$
            }
        }
        border |= BORDER_BOTTOM;

        addTab("   " + Messages.getString("juploadr.ui.label.family"), gc, family, 2, border); //$NON-NLS-1$ //$NON-NLS-2$
    }

    private void addFriendsTab(GC gc) {
        int border = BORDER_LEFT;
        if (!attrs.isFamilyViewable()) {
            border |= BORDER_BOTTOM;
        }
        addTab("   " + Messages.getString("juploadr.ui.label.friends"), gc, //$NON-NLS-1$ //$NON-NLS-2$
                friends, 1, border);
    }

    private void addPublicTab(GC gc) {
        addTab(Messages.getString("juploadr.ui.label.public"), gc, publicColor, //$NON-NLS-1$
                0, BORDER_LEFT | BORDER_BOTTOM);
    }

    private void addPrivateTab(GC gc) {
        int border = BORDER_LEFT | BORDER_BOTTOM;

        addTab(Messages.getString("juploadr.ui.label.private"), gc, //$NON-NLS-1$
                privateColor, 0, border);
    }

    private void addTagsTab(GC gc) {
        int border = BORDER_LEFT;
        if (!attrs.hasTitle()) {
            border |= BORDER_BOTTOM;
        }
        if (attrs.isPubliclyVisible() || !(attrs.isFamilyViewable())) {
            border |= BORDER_TOP;
        }

        addTab(TagParser.toInputString(attrs.getTags()), gc, description, 3, border);
    }

    private void addTitleTag(GC gc) {
        int border = BORDER_LEFT;
        if (!attrs.hasTags()) {
            border |= BORDER_TOP;
        }
        if (!attrs.hasDescription()) {
            border |= BORDER_BOTTOM;
        }
        addTab(attrs.getTitle(), gc, titleColor, 4, border);
    }

    private void addDescriptionTab(GC gc) {
        int border = BORDER_LEFT | BORDER_BOTTOM;
        if (!attrs.hasTitle()) {
            border |= BORDER_TOP;
        }

        addTab(attrs.getDescription(), gc, tags, 5, border);
    }

    /**
     * @param gc
     */
    private void addTab(String message, GC gc, Color bgColor, int offset, int borders) {
        if (!(attrs.getState() == UploadImage.STATE_UPLOADED || attrs.getState() == UploadImage.STATE_UPLOADING)) {
            ImageData data = new ImageData(TAB_WIDTH, TAB_HEIGHT, 24, new PaletteData(0, 0, 0));
            if (displayText) {
                data.alpha = 180;
            } else {
                data.alpha = 64;
            }
            Image tab = new Image(uiFactory.getDisplay(), data);

            Color borderColor = uiFactory.getTagBorderColor();
            Color white = uiFactory.getWhiteColor();

            GC tabGC = new GC(tab);

            tabGC.setFont(uiFactory.getThumbnailDecorationFont());
            tabGC.setBackground(bgColor);
            tabGC.fillRectangle(0, 0, TAB_WIDTH, TAB_HEIGHT);

            tabGC.setForeground(white);

            if (displayText) {
                tabGC.drawString(truncateText(message, tabGC), 2, 1);
                tabGC.setForeground(borderColor);

                if ((borders & BORDER_LEFT) != 0) {
                    tabGC.drawLine(0, 0, 0, TAB_HEIGHT);
                }
                if ((borders & BORDER_TOP) != 0) {
                    // draw a line across the top
                    tabGC.drawLine(0, 0, TAB_WIDTH, 0);
                }

                if ((borders & BORDER_BOTTOM) != 0) {

                    tabGC.drawLine(0, TAB_HEIGHT - 1, TAB_WIDTH, TAB_HEIGHT - 1);
                }
            }
            gc.drawImage(tab,BORDER_WIDTH + 1, BORDER_WIDTH
                    + (offset * TAB_HEIGHT));
            tabGC.dispose();
            tab.dispose();
        }
    }

    private String truncateText(String orig, GC gc) {
        FontMetrics metrics = gc.getFontMetrics();
        int stringWidth = gc.textExtent(orig).x;
        int imageWidth = thumb.getImage().getImageData().width;

        if (stringWidth > imageWidth) {
            StringBuffer sb = new StringBuffer();

            stringWidth = 0;
            int charPos = 0;
            sb.append("..."); //$NON-NLS-1$
            while (stringWidth < imageWidth - metrics.getAverageCharWidth()) {
                sb.insert(sb.length() - 3, orig.charAt(charPos));
                stringWidth = gc.textExtent(sb.toString()).x;
                charPos++;
            }
            return sb.toString();
        }
        return orig;
    }

}